package com.cems.config;

import com.cems.pojo.ComUser;
import com.cems.pojo.SysAdmin;
import com.cems.pojo.SysUser;
import com.cems.service.AdminService;
import com.cems.service.UserService;
import com.cems.util.ShiroMd5Util;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @ClassName UserController
 * @Author 陈新予
 * @Date 2021/4/16 15:22
 * @Version 1.0
 */
public class MyRealm extends AuthorizingRealm {
    @Autowired
    AdminService adminService;

    @Autowired
    UserService userService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        //授权
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Subject subject = SecurityUtils.getSubject();
        Session session = subject.getSession();
        Object loginAdmin = session.getAttribute("loginAdmin");
        if (loginAdmin instanceof SysAdmin) {
            SysAdmin admin = (SysAdmin) loginAdmin;
            info.addStringPermission(admin.getAdminLevel());
        }
        info.addStringPermission("loginOK");
        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("=====================管理员认证========================");
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;
        String username = userToken.getUsername();
        String realmName = getName();
        List<SysAdmin> admins = adminService.getAdmins();
        for (SysAdmin admin : admins) {
            if (admin.getAdminNum().equals(username)) {
                if (!admin.getAdminStatus().equals("正常")) {
                    throw new LockedAccountException();
                }
                Subject subject = SecurityUtils.getSubject();
                Session session = subject.getSession();
                session.setAttribute("isAdmin", true);
                session.setAttribute("loginAdmin", admin);
                //盐值
                ByteSource salt = ByteSource.Util.bytes(admin.getAdminNum());
                return new SimpleAuthenticationInfo(admin, admin.getAdminPwd(), salt, realmName);
            }
        }
        System.out.println("===============================开始用户认证===============================");
        List<SysUser> users = userService.getUser();
        for (SysUser user : users) {
            if (user.getUserPhone().equals(username)) {
                System.out.println("用户已确认!!!");
                if (!user.getStatus().equals("正常")) {
                    throw new LockedAccountException();
                }
                Subject subject = SecurityUtils.getSubject();
                Session session = subject.getSession();
                session.setAttribute("user", user);
                ComUser comUser = userService.selOneUser(user.getId());
                SysUser sysUser = userService.selOneSysUser(user.getId());
                session.setAttribute("usPname", comUser.getUserPname());
                session.setAttribute("userId",sysUser.getId());
                session.setAttribute("isAdmin", false);
                //盐值
                ByteSource salt = ByteSource.Util.bytes(user.getUserPhone());
                return new SimpleAuthenticationInfo(user, user.getUserPwd(), salt, realmName);
            }
        }
        return null;
    }

}
